package com.bigzero.workflow.modules.process.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigzero.workflow.common.service.TableService;
import com.bigzero.workflow.modules.process.dao.DeployDao;
import com.bigzero.workflow.modules.process.entity.DeployEntity;
import com.bigzero.workflow.modules.process.entity.DeployNodeEntity;
import com.bigzero.workflow.modules.process.service.DeployNodeService;
import com.bigzero.workflow.modules.process.service.DeployService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 流程部署详情实现
 * @author fanxinxiong
 * @date 2023-02-12 15:31
 **/
@Service
public class DeployServiceImpl extends ServiceImpl<DeployDao, DeployEntity> implements DeployService{

    @Autowired
    private DeployNodeService deployNodeService;

    @Autowired
    private TableService tableService;

    /**
     * 插入数据到绑定数据库表中
     *
     * @param instanceId 实例id
     * @param deployId   部署id
     * @param activityId 流程定义节点唯一标识
     * @param variables  流程变量
     */
    @Override
    public void saveData(String instanceId, String deployId, String activityId, Map<String, Object> variables) {
        DeployEntity deploy = this.getById(deployId);
        //如果没有找到数据就说明这个流程没有绑定数据库表
        if(deploy == null) return;
        DeployNodeEntity deployNode = deployNodeService.getOne(new LambdaQueryWrapper<DeployNodeEntity>()
                .eq(DeployNodeEntity::getActivityId, activityId)
                .eq(DeployNodeEntity::getDeployId, deployId));
        if (deployNode != null && deployNode.getColumns() != null) {
            // 更新数据
            tableService.saveOrUpdateData(instanceId, deploy.getTableName(), deployNode.getColumns(), variables);
        }
    }
}
